﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WPF_SFrost
{
    public partial class FormGroupAdd : Form
    {
        public FormGroupAdd()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckGroupName(txtName.Text))
                return;
            string sql = "insert into contactgroup values(@groupname,@memo)";
            SqlParameter[] parameters =
            {
                new SqlParameter("@groupname",txtName.Text.Trim()),
                new SqlParameter("@memo",txtRemark.Text.Trim())
            };
            int n = SqlDbHelper.ExecuteNonQuery(sql, CommandType.Text, parameters);
            if (n == 1)
            {
                MessageBox.Show("添加分类成功。");
            }
            else
            {
                MessageBox.Show("添加分类失败！");
            }
            this.Close();
        }

        public bool CheckGroupName(string groupName)
        {
            bool check = true;
            if (groupName == "")
            {
                MessageBox.Show("分组名称不能为空。");
                txtName.Focus();
                check = false;
            }
            else
            {
                string sql = "select count(*) from contactgroup where groupname = @groupname";
                SqlParameter[] parameters =
                {
                    new SqlParameter("@groupname",SqlDbType.VarChar,50)
                };
                parameters[0].Value = groupName;
                int n = (int)SqlDbHelper.ExecuteScalar(sql, CommandType.Text, parameters);
                if (n > 0)
                {
                    MessageBox.Show("分组已存在，请修改。");
                    txtName.Focus();
                    check = false;
                }
            }
            return check;
        }

        private void FormGroupAdd_Load(object sender, EventArgs e)
        {

        }
    }
}
